package at.technikum.fotoalbum.beans;

import java.util.ArrayList;
import java.util.Iterator;

import at.technikum.fotoalbum.database.DatabaseConnector;
import at.technikum.fotoalbum.utils.Utils;

public class CacheBean {

	private ArrayList<MainPictures> mainThumbPicture;

	public CacheBean() {
		DatabaseConnector dbConn = new DatabaseConnector();
		ArrayList<Image> allImages = dbConn.getAllImages();
		Iterator it = allImages.iterator();
		//String[] allPictures = {"","","","","",""};
		ArrayList<String> alPic = new ArrayList<String>();
		int x = 0;
		while (it.hasNext()) {
			alPic.add((String) ((Image)it.next()).getPath());
			x++;
		}
		String[] allPictures =  alPic.toArray(new String[alPic.size()]);
		/* TODO: Datenbankabfrage alle Bilder */
		/*
		String[] allPictures = { "/resources/pictures/tomahawk.jpg",
				"/resources/pictures/tomahawk2.jpg",
				"/resources/pictures/tomahawk3.jpg",
				"/resources/pictures/tomahawk4.jpg" };
		*/
		/* Ende Dummybefüllung */
		
		//mix up the pictures
		allPictures = Utils.mixUpArray(allPictures);
		String [] allThumbPictures = allPictures.clone();		
		for (int i = 0; i < allThumbPictures.length; i++) {
			allThumbPictures[i] = allThumbPictures[i].replace("/pictures/",
					"/pictures/thumbnail_");
		}

		mainThumbPicture = new ArrayList<MainPictures>();
		MainPictures mtp = new MainPictures();
		for (int i = 0; i < allThumbPictures.length; i++) {
			if (i == 0 || i % 3 == 0) {
				mtp = new MainPictures();
				mtp.setPic1(allThumbPictures[i]);
				mtp.setPic1_1(allPictures[i]);
			} else if (i % 3 == 1) {
				mtp.setPic2(allThumbPictures[i]);
				mtp.setPic2_1(allPictures[i]);
			} else if (i % 3 == 2) {
				mtp.setPic3(allThumbPictures[i]);
				mtp.setPic3_1(allPictures[i]);
				mainThumbPicture.add(mtp);
			}
			if (i == allPictures.length - 1 && i % 3 != 2) {
				mainThumbPicture.add(mtp);
			}
		}
	}

	public ArrayList<MainPictures> getMainPictureCache() {
		return mainThumbPicture;
	}

	public void setMainPictureCache(ArrayList<MainPictures> mainPictureCache) {
		this.mainThumbPicture = mainPictureCache;
	}

}
